package cn.edu.cqvie.atc.sort.impl;

import java.util.Comparator;

/**
 * 冒泡排序 << 简单排序 时间复杂度 O(n2)  >>
 *
 * @author Zheng Shaohong
 */
public class BubbleSort<T> extends BaseSort<T> {

    public BubbleSort() {

    }

    public static void main(String[] args) {

        BubbleSort<Integer> bubbleSort = new BubbleSort<>();
        Integer[] arr = new Integer[]{1, 3, 4, 5, 10, 9};
        bubbleSort.display(arr);
        System.out.println();
        bubbleSort.sort(arr, new IntegerComparator());
        bubbleSort.display(arr);

    }

    @Override
    public void sort(T[] arr, Comparator<T> c) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = arr.length - 1; j > i; j--) {
                if (c.compare(arr[j], arr[j - 1]) > 0) {
                    T temp = null;
                    temp = arr[j];
                    arr[j] = arr[j - 1];
                    arr[j - 1] = temp;
                    // help gc
                    temp = null;
                }
            }
        }

    }

}
